package com.org.base.widget;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.view.ViewPager;
import android.text.method.SingleLineTransformationMethod;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.org.zp.R;

public class PageControlView extends LinearLayout implements PageIndicator {
	private Context context;
	private ViewPager mViewPager;
	private ViewPager.OnPageChangeListener mListener;
	private int mCurrentPage = 0;
	private int mScrollState;
	private int mCount;
	private TextView mPageTitle;

	public PageControlView(Context context) {
		super(context);
		this.init(context);
	}

	public PageControlView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.init(context);
	}

	private void init(Context context) {
		this.context = context;
		setBackgroundColor(0);
	}

	private void generatePageControl(int currentIndex) {
		this.removeAllViews();

		int pageNum = 6; // 显示多少个
		int pageNo = currentIndex + 1; // 第几页
		int pageSum = this.mCount; // 总共多少页

		if (pageSum > 1) {
			int currentNum = (pageNo % pageNum == 0 ? (pageNo / pageNum) - 1
					: (int) (pageNo / pageNum)) * pageNum;
			if (currentNum < 0)
				currentNum = 0;
//			if (pageNo > pageNum) {
//				ImageView imageView = new ImageView(context);
//				imageView.setImageResource(R.drawable.jump_right);
//				this.addView(imageView);
//			}
			for (int i = 0; i < pageNum; i++) {
				if ((currentNum + i + 1) > pageSum || pageSum < 2)
					break;
				ImageView imageView = new ImageView(context);
				if (currentNum + i + 1 == pageNo) {
					imageView.setImageResource(R.drawable.slide_1);
				} else {
					imageView.setImageResource(R.drawable.slide_2);
				}
				LayoutParams params = new LayoutParams(
						LinearLayout.LayoutParams.WRAP_CONTENT,
						LinearLayout.LayoutParams.WRAP_CONTENT);
				params.leftMargin = 20;
				params.topMargin = 20;
				this.addView(imageView, params);
			}
//			if (pageSum > (currentNum + pageNum)) {
//				ImageView imageView = new ImageView(context);
//				imageView.setImageResource(R.drawable.jump_right);
//				this.addView(imageView);
//			}
		}
	}

	@Override
	public void setViewPager(ViewPager view) {
		if (view.getAdapter() == null) {
			throw new IllegalStateException(
					"ViewPager does not have adapter instance.");
		}
		mViewPager = view;
		mViewPager.setOnPageChangeListener(this);
		mCount = mViewPager.getAdapter().getCount();
		updatePageState();
		invalidate();
	}

	@Override
	public void setViewPager(ViewPager view, int initialPosition) {
		mCurrentPage = initialPosition;
		setViewPager(view);
		setCurrentItem(initialPosition);
	}

	@Override
	public void setCurrentItem(int item) {
		if (mViewPager == null) {
			throw new IllegalStateException("ViewPager has not been bound.");
		}
		mViewPager.setCurrentItem(item);
		mCurrentPage = item;
		invalidate();
	}

	@Override
	public void onPageScrollStateChanged(int state) {
		mScrollState = state;

		if (mListener != null) {
			mListener.onPageScrollStateChanged(state);
		}
		if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
			updatePageState();
		}
	}

	public void updatePageState() {
		generatePageControl(mCurrentPage);
		TitleProvider titleProvider = (TitleProvider) mViewPager.getAdapter();
		mPageTitle.setText(titleProvider.getTitle(mCurrentPage));
		mPageTitle.setTransformationMethod(SingleLineTransformationMethod.getInstance());
		mPageTitle.setFocusable(true);
	}

	@Override
	public void onPageScrolled(int position, float positionOffset,
			int positionOffsetPixels) {
		mCurrentPage = position;
		invalidate();

		if (mListener != null) {
			mListener.onPageScrolled(position, positionOffset,
					positionOffsetPixels);
		}
	}

	@Override
	public void onPageSelected(int position) {
		if (mScrollState == ViewPager.SCROLL_STATE_IDLE) {
			mCurrentPage = position;
			invalidate();
		}
		if (mListener != null) {
			mListener.onPageSelected(position);
		}
	}

	@Override
	public void setOnPageChangeListener(ViewPager.OnPageChangeListener listener) {
		mListener = listener;
	}

	@Override
	public void onRestoreInstanceState(Parcelable state) {
		SavedState savedState = (SavedState) state;
		super.onRestoreInstanceState(savedState.getSuperState());
		mCurrentPage = savedState.currentPage;
		requestLayout();
	}

	@Override
	public Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		SavedState savedState = new SavedState(superState);
		savedState.currentPage = mCurrentPage;
		return savedState;
	}

	static class SavedState extends BaseSavedState {
		int currentPage;

		public SavedState(Parcelable superState) {
			super(superState);
		}

		private SavedState(Parcel in) {
			super(in);
			currentPage = in.readInt();
		}

		@Override
		public void writeToParcel(Parcel dest, int flags) {
			super.writeToParcel(dest, flags);
			dest.writeInt(currentPage);
		}

		public static final Parcelable.Creator<SavedState> CREATOR = new Parcelable.Creator<SavedState>() {
			@Override
			public SavedState createFromParcel(Parcel in) {
				return new SavedState(in);
			}

			@Override
			public SavedState[] newArray(int size) {
				return new SavedState[size];
			}
		};
	}

	public void setPageTitle(TextView mPageTitle) {
		this.mPageTitle = mPageTitle;
	}
}
